package com.ruoyi.web.controller.quality;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.excel.ExcelResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.listener.classification.UserClassificationImportListener;
import com.ruoyi.quality.domain.bo.QualityUserClassificationBo;
import com.ruoyi.quality.domain.vo.*;
import com.ruoyi.quality.service.IQualityUserClassificationService;
import com.ruoyi.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * 用户分级
 *
 * @author ruoyi
 * @date 2023-12-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/quality/userClassification")
public class QualityUserClassificationController extends BaseController {

    private final IQualityUserClassificationService iQualityUserClassificationService;

    private final ISysUserService userService;

    /**
     * 查询用户分级列表
     */
    @SaCheckPermission("quality:userClassification:list")
    @GetMapping("/list")
    public TableDataInfo<QualityUserClassificationVo> list(QualityUserClassificationBo bo, PageQuery pageQuery) {
        SysUser userBo = BeanUtil.toBean(bo, SysUser.class);
        if (!Objects.isNull(userBo)){
            String[] UserNameList = userService.selectUserNameList(userBo);
            bo.setUserNameList(UserNameList);
        }
        TableDataInfo<QualityUserClassificationVo> list = iQualityUserClassificationService.queryPageList(bo, pageQuery);
        List<QualityUserClassificationVo> voList = list.getRows();
        voList.forEach(item->{
            String userName = item.getUserName();
            SysUser user = userService.selectUserByUserName(userName);
            ClassificationDataUser classDataUser = BeanUtil.toBean(user, ClassificationDataUser.class);
            item.setClassDataUser(classDataUser);
        });
        list.setRows(voList);
        return list;
    }

    /**
     * 导出全部用户分级列表
     */
    @SaCheckPermission("quality:userClassification:export")
    @Log(title = "用户分级", businessType = BusinessType.EXPORT)
    @PostMapping("/exportAll")
    public void exportAll(QualityUserClassificationBo bo, HttpServletResponse response) {
        List<QualityUserClassificationVo> list = iQualityUserClassificationService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户分级", QualityUserClassificationVo.class, response);
    }

    /**
     * 导出选择用户分级列表
     */
    @SaCheckPermission("quality:userClassification:export")
    @Log(title = "用户分级", businessType = BusinessType.EXPORT)
    @PostMapping("/exportSelect/{ids}")
    public void exportSelect(@NotEmpty(message = "主键不能为空")
                                 @PathVariable Collection<Long> ids, HttpServletResponse response) {
        List<QualityUserClassificationVo> list = iQualityUserClassificationService.queryByIds(ids);
        ExcelUtil.exportExcel(list, "用户分级", QualityUserClassificationVo.class, response);
    }

    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "项目得分",  QualityUserClassificationVo.class, response);
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @SaCheckPermission("quality:userClassification:import")
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        ExcelResult<QualityUserClassificationImportVo> result = ExcelUtil.importExcel(file.getInputStream(), QualityUserClassificationImportVo.class, new UserClassificationImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }

    /**
     * 获取用户分级详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("quality:userClassification:query")
    @GetMapping("/{id}")
    public R<QualityUserClassificationVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iQualityUserClassificationService.queryById(id));
    }

    /**
     * 新增用户分级
     */
    @SaCheckPermission("quality:userClassification:add")
    @Log(title = "用户分级", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody QualityUserClassificationBo bo) {
        return toAjax(iQualityUserClassificationService.insertByBo(bo));
    }

    /**
     * 修改用户分级
     */
    @SaCheckPermission("quality:userClassification:edit")
    @Log(title = "用户分级", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody QualityUserClassificationBo bo) {
        return toAjax(iQualityUserClassificationService.updateByBo(bo));
    }

    /**
     * 删除用户分级
     *
     * @param ids 主键串
     */
    @SaCheckPermission("quality:userClassification:remove")
    @Log(title = "用户分级", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iQualityUserClassificationService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
